package org.jackysoft.controller;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jackysoft.annotations.Logger;
import org.jackysoft.controller.base.AbstractController;
import org.jackysoft.entity.User;
import org.jackysoft.service.UserService;
import org.jackysoft.util.OperationType;
import org.jackysoft.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;



@Logger(label = "用户", logType = OperationType.System)
@Controller
@RequestMapping("/user")
public class UserController extends AbstractController<User> {

	@Resource
	private UserService service;
	
	
	@RequestMapping("/removeauth")
	public void removeauth(String username,String role) throws Exception{
		
		service.removeAuthority(username, role);
		
	}
	
	@RequestMapping("/removerole")
	public void removerole(String name) throws Exception{
		service.removeRole(name);
		
	}
	
	@RequestMapping("/removedepartment")
	public void removedepartment(String name) throws Exception{
		service.removeDepartment(name);

	}
	
	@RequestMapping("/ofusers")
	public void ofusers(String role,int page,int rows,@ModelAttribute Page pager) throws Exception{
		
		pager = service.findBySecurityRole(role, page, rows);
		
	}
	
	public void acegi() throws Exception {
		
	}

	@RequestMapping("/samedept")
	public void samedept(@ModelAttribute Collection<User> beans) throws Exception {
		beans = service.sameDepartment();
	
	}

	
	@RequestMapping("/saveprofile")
	public void saveprofile(@ModelAttribute User bean) throws Exception {
		service.saveProfile(bean);
		
	}

	@RequestMapping("/bydepartment")
	public void bydepartment(String dept,int page,int rows,@ModelAttribute Page pager) throws Exception {
		pager = service.findByDepartment(dept, page, rows);			
		
	}

	@RequestMapping("/byrole")
	public void byrole(String role,int page,int rows,@ModelAttribute Page pager) throws Exception{		
		pager = service.findByRole(role, page, rows);			
		
	}
	
	@ResponseBody 
	@RequestMapping("/preuser/{username}")
	public Map<String,Boolean> checkin(@PathVariable String username) throws Exception {
		Map<String,Boolean> hasin = Maps.newHashMap();
		hasin.put("hasin",service.exists(username));
		return hasin;
	}

	@RequestMapping("/passall")
	public void passall(List<String> names) throws Exception {
		service.pass(names);	
	}
	
	@RequestMapping("/search")
	public void search(String search,@ModelAttribute Collection<User> beans) throws Exception {
		beans = service.searchs(search);
		
	}
	
}